<?php
/**
 * Created by PhpStorm.
 * User: guokunsheng
 * Date: 2019/1/4
 * Time: 12:03
 */

namespace app\api\controller\v6;

use app\api\controller\v2\Auth;
use app\common\lib\horoscope\Horoscope as HoroscopeData;
use app\common\lib\Util;
use app\common\model\Recode;

class Horoscope extends Auth
{
    /**
     * 八字页面
     * @return \think\response\Json
     * @throws \app\common\lib\exception\ApiException
     */
    public function index()
    {
        $rid = input('id/d', 0);
        $time_start = input('time_start/d', 23);
        $time_type = input('time_type/d', 0);

        if (!in_array($time_start, [0, 23]))
        {
            $time_start = 0;
        }

        if (!in_array($time_type, [0, 1]))
        {
            $time_start = 0;
        }

        $recode = (new Recode())->getRecode(['id' => $rid, 'uid' => $this->user['id']]);
        if (!$recode || $recode['uid'] != $this->user['id'])
        {
            return show(config('code.error'), '档案不存在');
        }

        $now = time();

        $advance = [
            'year' => date('Y', $now) * 1,
            'month' => date('m', $now) * 1,
            'day' => date('d', $now) * 1,
            'hour' => date('H', $now) * 1,
            'minute' => date('i', $now) * 1,
            'timezone' => 8,
            'is_summer' => 0,
        ];

        $option = [
            'recode' => $recode,
            'time_start' => $time_start,
            'time_type' => $time_type,
            'advance' => $advance,
        ];

        $data = HoroscopeData::getInstance($option)->getUserData();

        // 0=>木,1=>火,2=>土,3=>金,4=>水 2022-08-29 修改五行比例逻辑，仅限星盘中的八字页面数据更新
        $wxw = HoroscopeData::getInstance()->getWuxingWang();
        $wx_arr = ['木', '火', '土', '金', '水'];
        $sum = array_sum($wxw);

        foreach ($wxw as $k => $v)
        {
            $data['wuxing'][$wx_arr[$k]] = round($v/$sum * 100);
        }

        if ($recode['timezone'])
        {
            if ($recode['timezone'] > 0)
            {
                $timezone = '（东' . (new Util())->numberChinese($recode['timezone']) . '区 GMT+' . $recode['timezone'] . '）';
            } else
            {
                $timezone = '（西' . (new Util())->numberChinese(abs($recode['timezone'])) . '区 GMT' . $recode['timezone'] . '）';
            }
        } else
        {
            $timezone = '（中时区 GMT0）';
        }

        $data['birth_time'] .= $timezone;

        return show(config('code.success'), '', $data);
    }
}
